package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;

import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmDownOrUpDto;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author 黄晏凯
 */
@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    /**
     * 根据参数加载文章列表
     *
     * @param dto  dto
     * @param type 类型
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, byte type) {
        List<ApArticle> list = apArticleMapper.load(dto, type);
        return ResponseResult.okResult(list);
    }

    /**
     * 保存app端相关文章
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) throws TemplateException, IOException {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        //2.判断是否存在id
        if (null == dto.getId()) {
            apArticleMapper.insert(apArticle);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContentMapper.insert(apArticleContent);

            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

        } else {
            apArticleMapper.updateById(apArticle);
            //修改文章内容
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(
                    Wrappers.<ApArticleContent>lambdaQuery()
                            .eq(ApArticleContent::getArticleId, apArticle.getId())
            );
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //生成静态页面-使用异步的方式来生成
        articleFreemarkerService.buildArticleToMinIO(apArticle.getId());

        //3.结果返回  文章的id
        return ResponseResult.okResult(apArticle.getId());
    }

    /**
     * 更新上下架
     *
     * @param msg msg
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult updateIsDown(String msg) {
        WmDownOrUpDto wmDownOrUpDto = JSON.parseObject(msg, WmDownOrUpDto.class);
        ApArticleConfig apArticleConfig = new ApArticleConfig(wmDownOrUpDto.getArticleId());
        apArticleConfig.setIsDown(wmDownOrUpDto.getIsDown());
        LambdaUpdateWrapper<ApArticleConfig> set = Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId, apArticleConfig.getArticleId())
                .set(ApArticleConfig::getIsDown, apArticleConfig.getIsDown());
        apArticleConfigMapper.update(apArticleConfig, set);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}